<template>
	<view>
		<up-navbar leftIconColor="#FFFFFF" leftIconSize="44rpx" :title="title" :autoBack="true" placeholder
			bgColor="transparent">
		</up-navbar>
		<up-empty v-if="result.list.length ===0 " mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png">
		</up-empty>
		<view class="map-item" v-for="item in result.list" :key="item.id">
			<deaftIndex ad showImg :item="item" @changeGoodStatus="changeGoodStatus" @details="adDetails" type='dot' />
		</view>
		<up-gap height="240rpx"></up-gap>
		<view class="v-button" style="bottom: 110rpx;" @click="addContribute">发布投稿</view>
		<up-back-top :scrollTop="scrollTop" :iconStyle="iconStyle" :duration="200" :bottom="160"></up-back-top>
	</view>
</template>

<script setup>
	import secondApi from "../../api/biz/second"
	import openApi from "@/api/biz/open.js"
	import deaftIndex from "@/components/draft/index.vue"
	import {
		msg,
		notLogged
	} from "@/utils/tool.js"
	import {
		onLoad,
		onShow,
		onReachBottom,
		onPageScroll
	} from "@dcloudio/uni-app"
	import {
		ref,
		reactive
	} from "vue"

	const iconStyle = {
		fontSize: '32rpx',
		color: '#2979ff'
	};
	const scrollTop = ref(0);
	onPageScroll((e) => {
		scrollTop.value = e.scrollTop;
	})

	onReachBottom(() => {
		if (result.list.length >= result.total) return;
		params.pageNo++;
		getStrategyPage()
	})

	function changeGoodStatus(id) {
		let index = result.list.findIndex(item => item.id === id);
		if (result.list[index].goodStatus) result.list[index].goodCount--;
		else result.list[index].goodCount++;
		result.list[index].goodStatus = !result.list[index].goodStatus
	}


	const params = reactive({
		pageNo: 1,
	})

	const result = reactive({
		list: [],
		total: 0
	})

	function getStrategyPage() {
		openApi.getStrategyPage({
			pageNo: params.pageNo,
			pageSize: 10,
			type: 1,
			typeId: id.value
		}).then(res => {
			if (res.code === 0) {
				result.total = res.data.total;
				if (params.pageNo === 1) result.list = res.data.list;
				else result.list = result.list.concat(res.data.list)
			}
		})
	}

	function addContribute() {
		if (!uni.getStorageSync('accessToken')) return notLogged()
		uni.navigateTo({
			url: `/contribute/create/index?id=${id.value}&name=${title.value}`
		})
	}

	const title = ref("")
	const id = ref()

	let rewardedVideoAd = null

	const detailsId = ref(null);
	const adDetails = (id) => {
		detailsId.value = id;

		const adTime = uni.getStorageSync('adTime') || 0;
		const adTimeLength = 1000 * 60 * 5; // 5分钟

		if (!adStatus.value || (adTime + adTimeLength) > new Date().valueOf()) joinDetails()
		else rewardedVideoAd.show();

		// joinDetails()
	}

	const joinDetails = () => {
		uni.navigateTo({
			url: "/strategy/details/index?id=" + detailsId.value
		})
	}

	const adInit = () => {
		let isEnded = -1;

		if (wx.createRewardedVideoAd) {
			rewardedVideoAd = wx.createRewardedVideoAd({
				adUnitId: 'adunit-366ba1a04b3a14f0'
			})
			rewardedVideoAd.onLoad(() => {
				adStatus.value = true;
				console.log('初始化完成')
			})
			rewardedVideoAd.onError((err) => {
				adStatus.value = false;
				console.log('广告加载失败', err)
			})
			rewardedVideoAd.onClose((res) => {
				if (res?.isEnded) {
					isEnded = 0

					uni.setStorageSync('adTime', new Date().valueOf())
					secondApi.createUserPoint()
					joinDetails()
				} else {
					console.log("用户中断")
					isEnded = 1;
				}
			})
		}
	}

	const adStatus = ref(false);

	onLoad((option) => {
		title.value = option.name
		id.value = option.id

		// 广告加载
		adInit()
	})

	onShow(() => {
		params.pageNo = 1;
		getStrategyPage()
	})
</script>
<style>
	page {
		background-color: #1B1829;
	}
</style>
<style lang="scss" scoped>
	.map-item {
		width: 690rpx;
		margin: 30rpx auto 0;
	}
</style>